from django.shortcuts import render
from rest_framework.generics import ListAPIView

# Create your views here.
from goods.serializers import SKUSerializers, SKUSearchSerializers
from goods.models import SKU
from goods.utils import StandardResultsSetPagination
from rest_framework.filters import OrderingFilter

from drf_haystack.viewsets import HaystackViewSet


class SKUListView(ListAPIView):
    serializer_class = SKUSerializers
    # queryset = SKU.objects.filter()  无法获取id值需要重写方法

    # 局部使用这个分页类
    pagination_class = StandardResultsSetPagination
    # 指定过滤排序的形式
    filter_backends = [OrderingFilter]
    ordering_fields = ['price', 'create_time', 'sales']

    def get_queryset(self):
        pk = self.kwargs['pk']
        return SKU.objects.filter(category_id=pk, is_launched=True)


class SKUSearchView(HaystackViewSet):

    pagination_class = StandardResultsSetPagination

    index_models = [SKU]

    serializer_class = SKUSearchSerializers